package gui.dialog;

import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import network.SendSignalException;

import controller.MessageController;
import controller.UserNotConnected;

import user.LocalUser;
import utils.Configuration;

public class ChatUsernameDialog extends ChatDialog implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final String USERNAME_TITLE = "Changer de login... " ;
	private static final Dimension USERNAME_SIZE = new Dimension(300, 200) ;
	
	private JButton changeUsernameButton ;
	private JButton cancelButton ;
	private JTextField fieldLogin ;
	private MessageController msgCtrl ;
	
	private KeyListener keyAdapater = new KeyAdapter() {
		@Override
		public void keyPressed(KeyEvent e) {
			switch (e.getKeyCode()) {
			case KeyEvent.VK_ENTER:
				ChatUsernameDialog.this.changeUsernameButton.doClick() ;
				break ;
			case KeyEvent.VK_ESCAPE:
				ChatUsernameDialog.this.cancelButton.doClick() ;
				break ;
			default:
			}
		}
	};
	
	private WindowListener windowAdapter = new WindowAdapter() {
		@Override
		public void windowActivated(WindowEvent e) {
			ChatUsernameDialog.this.fieldLogin.setText(LocalUser.getLocalUser().getUsername()) ;
			ChatUsernameDialog.this.fieldLogin.requestFocus() ;
		}
	};
	
	/**
	 * Create and return the information panel (Top of the dialog)
	 * 
	 * @return
	 */
	private JPanel createInfoPanel () {
		JPanel infoPanel = new JPanel() ;
		JLabel iconLabel = null ;
		try {
			iconLabel = new JLabel(new ImageIcon(ImageIO.read(new File(Configuration.getImageFolder() + File.separator + "connect_img.png")))) ;
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.X_AXIS)) ;
		infoPanel.add(Box.createRigidArea(new Dimension(10, 0))) ;
		if (iconLabel != null) {
			iconLabel.setSize(new Dimension(20, 20)) ;
			infoPanel.add(iconLabel) ;
			infoPanel.add(Box.createRigidArea(new Dimension(10, 0))) ;
		}
		infoPanel.add(new JLabel("<html><p>Merci de spécifier un login correct.</p></html>")) ;
		infoPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)) ;
		infoPanel.setAlignmentX(LEFT_ALIGNMENT) ;
		return infoPanel ;
	}
	
	/**
	 * Create and return the input panel (middle of the dialog).
	 * This functionc reate the JTextField attribute.
	 * 
	 * @return
	 */
	private JPanel createInputPanel () {
		JPanel loginPanel = new JPanel () ;
		this.fieldLogin = new JTextField(10) ;
		this.fieldLogin.addKeyListener(this.keyAdapater) ;
		loginPanel.setLayout(new BoxLayout(loginPanel, BoxLayout.X_AXIS)) ;
		loginPanel.setAlignmentX(LEFT_ALIGNMENT) ; 
		loginPanel.add(Box.createRigidArea(new Dimension(20,0))) ;
		loginPanel.add(new JLabel("<html><b>Login : </b></html>")) ;
		loginPanel.add(Box.createHorizontalGlue()) ;
		loginPanel.add(this.fieldLogin) ;
		loginPanel.add(Box.createRigidArea(new Dimension(20,0))) ;
		loginPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, 50)) ;
		return loginPanel ;
	}
	
	/**
	 * Create and return the buttons panel (bottom of the dialog).
	 * This function create the JButton attributes.
	 * 
	 * @return
	 */
	private JPanel createButtonsPanel () {
		JPanel buttonPanel = new JPanel () ;
		this.changeUsernameButton = new JButton("Valider") ;
		this.changeUsernameButton.addActionListener(this) ;
		this.cancelButton = new JButton("Annuler") ;
		this.cancelButton.addActionListener(this) ;
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)) ;
		buttonPanel.add(Box.createHorizontalGlue()) ;
		buttonPanel.add(this.changeUsernameButton) ;
		buttonPanel.add(Box.createHorizontalGlue()) ;
		buttonPanel.add(this.cancelButton) ;
		buttonPanel.add(Box.createHorizontalGlue()) ;
		buttonPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)) ;
		buttonPanel.setAlignmentX(LEFT_ALIGNMENT) ;
		return buttonPanel ;
	}
	
	public ChatUsernameDialog (Window master, MessageController msgCtrl) {
		super (master, USERNAME_TITLE, USERNAME_SIZE) ;
		this.msgCtrl = msgCtrl ;
		/* Create and place Component... */
		setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS)) ;
		add(createInfoPanel()) ;
		add(createInputPanel()) ;
		add(createButtonsPanel()) ;
		addWindowListener(this.windowAdapter) ;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.changeUsernameButton && !this.fieldLogin.getText().trim().isEmpty()) {
			try {
				this.msgCtrl.changeUsername(this.fieldLogin.getText().trim()) ;
			} 
			catch (UserNotConnected e1) {
			} 
			catch (SendSignalException e1) {
			}
		}
		this.setVisible(false) ;
	}

}
